var j = {};
j.init = function(){
    //初始化所有的日期选择控件
};

j.NewJud = function(opts,el){
    if(el){
        return new el.JUD(opts);
    }
    return new JUD(opts);
};
j.NewJd = function(opts){
    return new JD(opts);
};
j.alert = function(msg,isShowError){
    if(typeof top.show === "function"){
        if(isShowError){
            top.showError(msg);
            return;
        }
        top.show(msg);
    }else{
        //alert(msg);
        $.messager.show({
            title:'消息',
            msg:msg,
            showType:'show'
        });
    }
};
/**
 * 在弹出的窗口中，引用父窗口中内容的方法：
 * this.opener. + 父窗口元素 赋值给父窗口，在本封装集中，则是：this.opener.data.+所传递的data的数据
 * @param url
 * @param name 弹出窗口的名字，若名字相同，则二次弹窗将覆盖第一次的弹窗
 * @param data 需要将数据传给弹窗的数据，json格式
 * @param width 宽度，默认为300，只能是数字形式
 * @param height 调试，默认300，只能是数字形式
 */
j.open1 = function(url,name,data,width,height){
    if(typeof width === "undefined"){
        width = 300;
    }
    if(typeof height === "undefined"){
        height = 300;
    }
    var settings = "width="+width+",";      //指定宽度
    settings += "height="+height+",";        //指定高度
    settings += "toolbar=no,menubar=no,scrollbars=yes,status=no,resizable=yes,";//没有状态栏,没有帮助按钮,不能滚动,不能改变大小
    settings += "z-look=yes,alwaysRaised=yes,";
    var _left = screen.width/2-width/2;
    var _top = screen.height/2-height/2;
    settings += "left="+_left+",top="+_top; //居中 for firefox
    if(typeof data === "object"){
        window.data = data;
    }
    if(typeof name !== "string" || name.length == 0){
        name = null;
    }
    var openid = window.open(url,name,settings);
    openid && openid.focus();
};
/**
 * 在弹出的窗口中，引用父窗口中内容的方法：
 * this.opener. + 父窗口元素 赋值给父窗口，在本封装集中，则是：this.opener.data.+所传递的data的数据
 * @param url
 * @param name 弹出窗口的名字，若名字相同，则二次弹窗将覆盖第一次的弹窗
 * @param data 需要将数据传给弹窗的数据，json格式
 * @param width 宽度，默认为300，只能是数字形式
 * @param height 调试，默认300，只能是数字形式
 */
j.open = function(url,name,data,width,height){
    if(typeof width === "undefined"){
        width = 300;
    }
    if(typeof height === "undefined"){
        height = 300;
    }
    var _left = screen.width/2-width/2;
    var _top = screen.height/2-height/2;
    if(typeof window.showModalDialog === "function"){
        if(!data){
            data = {};
        }
        data.parent = window;
        return j.showModalDialog(url,data,width,height,_left,_top);
    }
    var settings = "width="+width+",";      //指定宽度
    settings += "height="+height+",";        //指定高度
    settings += "toolbar=no,menubar=no,scrollbars=yes,status=no,resizable=yes,";//没有状态栏,没有帮助按钮,不能滚动,不能改变大小
    settings += "z-look=yes,alwaysRaised=yes,";
    settings += "left="+_left+",top="+_top; //居中 for firefox
    if(typeof name !== "string" || name.length == 0){
        name = null;
    }
    var openid = window.open(url,name,settings);
    if(typeof data === "object"){
        openid.data = data;
    }
    openid && openid.focus();
};
j.showModalDialog = function(url,data,width,height,left,top){
    var settings = "dialogHeight:"+height+";dialogWidth:"+width+";dialogLeft:"+left+";dialogTop:"+top;
    window.showModalDialog(url,data,settings);
};
/**
 * 切换显示指定ID的dom元素
 * @param e
 * @param id
 * @param flag 若为1，则始终显示，不会隐藏
 */
j.toggle = function(e,id,flag){
    var d = $("#"+id);
    if(d.css("display") === "none"){
        d.css("display","block");
    }else if(flag == 0){
        d.css("display","none");
    }
};
j.showPopup = function(id,position){
    var d = $("#"+id);
    if(d.css("display") === "none"){
        d.css("display","block");
    }
    var p = position;
    if(p == null || typeof position === "undefined"){
        //居中显示
        p = {};
        p.left = ($(window).width() - d.outerWidth())/2;
        p.top = ($(window).height() - d.outerHeight())/2 + $(document).scrollTop();
    }
    d.css({"top":p.top+"px","left":p.left+"px"});
};
function imgOnError(sender,missedImg){
    sender.src=missedImg;
    sender.onerror=null; 控制不要一直跳动
}

/**显示用户信息**/
function showMemberInfo(sender,memberId){
    d.resetXY(sender);
    d.show("/user/center?act=minfo",{userId:memberId});
}
function showMultiMemberInfo(sender,memberIds){
    f.resetXY(sender);
    f.opend("/user/center",{act:"mmi",userIds:memberIds},"负责人",510,true);
}
function booleanFormatter(val,row){
    if(val){
        return "是";
    }
    return "否";
}
function timeFormatter(val,row){
    return val;
}
function getDataGridSelectedIds(datagridId,key){
    var ids = "";
    var rows = $("#"+datagridId).datagrid("getSelections");
    for(var i=0;i<rows.length;i++){
        if(i>0){
            ids+=",";
        }
        ids+=rows[i][key];
    }
    return ids;
}
function onOrgBeforeSelect(node){
    var attributes = node.attributes;
    if(attributes){
        var canSelect = attributes["canSelect"];
        if(typeof canSelect==="string" && canSelect === "1"){
            return true;
        }
    }
    alert("这是集团公司,不能选择!");
    return false;
}function onOrgBeforeSelect1(node){
    var attributes = node.attributes;
    if(attributes){
        var canSelect = attributes["canSelect"];
        if(typeof canSelect==="string" && canSelect === "1"){
            return true;
        }
    }
    return false;
}
function onMergeLoadSuccess(data){
    var $datagrid = $(this);
    var columnNames = $datagrid.datagrid("options")["mergeColumn"];
    if(columnNames.length>0){
        for(var i=0;i<columnNames.length;i++){
            doMerge($datagrid,data,columnNames[i]);
        }
    }
}
function doMerge($datagrid,data,columnName){
    var merges = getMerges(data,columnName);
    if(merges.length>0){
        for(var i=0; i<merges.length; i++){
            $datagrid.datagrid('mergeCells',{
                index: merges[i].index,
                field: columnName,
                rowspan: merges[i].rowspan
            });
        }
    }
}
function getMerges(data,columnName){
    var merges = [];
    if(data.total>0){
        var i = 0;
        var item;
        var map = [];
        var v ;
        var j = 0;
        var lastV = "";
        for(i=0;i<data.rows.length;i++){
            v = data.rows[i][columnName];
            if(v === null || typeof v === "undefined" || v === ""){
                continue;
            }
            if(lastV === ""){
                lastV = v;
            }
            item = map[j];
            if(item === undefined){
                //这是第一次
                item = {index:i,rowspan:1};
                map[j]=item;
            }else{
                if(v === lastV){
                    item.rowspan+=1;
                }else{
                    j+=1;
                    map[j] = {index:i,rowspan:1};
                }
            }
            lastV = v;
        }
        if(map.length>0){
            j = 0;
            for(i=0;i<map.length;i++){
                item = map[i];
                if(item.rowspan===1){
                    continue;
                }
                merges[j++]=item;
            }
        }
    }
    return merges;
}
function getMerges1(data,columnName){
    var merges = [];
    if(data.total>0){
        var i = 0;
        var item;
        var map = new Map();
        var v ;
        for(i=0;i<data.total;i++){
            var row = data.rows[i];
            v = row[columnName];
            if(v == null || typeof v == "undefined"){
                continue;
            }
            item = map.get(v);
            if(item === undefined){
                item = {key:v,index:i,rowspan:1};
                map.put(v,item);
            }else{
                item.rowspan+=1;
            }
        }
        if(map.size()>0){
            var j = 0;
            for(i=0;i<map.size();i++){
                item = map.element(i).value;
                if(item.rowspan===1){
                    continue;
                }
                merges[j++]={index:item.index,rowspan:item.rowspan};
            }
        }
    }
    return merges;
}
function comdifyFormatter(number){
    var numberStr = "";
    if(null==numberStr || typeof number === "undefined"){
        return;
    }
    numberStr = String(number);
    var num = parseFloat(numberStr);
    if(isNaN(num)){
        return number;
    }
    numberStr = numberStr.replace(/,/g, "");
    if (numberStr.length > 10)
    {
        numberStr = numberStr.substring(0, 10);
    }
    var re=/\d{1,3}(?=(\d{3})+$)/g;
    var n1=numberStr.replace(/^(\d+)((\.\d+)?)$/,function(s,s1,s2){return s1.replace(re,"$&,")+s2;});
    return n1;
}
function getComboValues(key){
    var ids = $("#"+key).combobox("getValues");
    var _ids = "";
    if(ids.length>0){
        for (var i = 0; i < ids.length; i++) {
            if(i>0){
                _ids+=",";
            }
            _ids+=ids[i];
        }
    }
    return _ids;
}
function doGridCellTip($dg){
    window.setTimeout(function(){
        $dg.datagrid('doCellTip',{
            onlyShowInterrupt:true,//只显示被截断的单元格的内容
            position:'bottom',
            tipStyler:{'backgroundColor':'#fff000', borderColor:'#ff0000', maxWidth:'300px', boxShadow:'1px 1px 3px #292929'},
            contentStyler:{paddingLeft:'5px'}
        });
    },500);
}
function onLoadDataSuccess(data){
    if(data.rows.length>0){
        doGridCellTip($(this));
    }
}
function booleanFormmater(val,row){
    if(val === "1" || val == true || val === "true"){
        return "是";
    }
    return "否";
}